<template>
  <div class="xtx-city">
    <div class="select" @click="toggleDialog" :class="{ active: active }">
      <span class="placeholder" v-if="!changeResult.resultAddress"
        >请选择配送地址</span
      >
      <span class="value" v-else>{{ changeResult.resultAddress }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="active">
      <span
        @click="optionClick(i)"
        class="ellipsis"
        v-for="i in dataList"
        :key="i.code"
        >{{ i.name }}</span
      >
    </div>
  </div>
</template>
<script>
import axios from 'axios'
import { reactive, ref, watch } from 'vue'
export default {
  name: 'XtxCity',
  setup(props, context) {
    const active = ref(false)
    const open = () => {
      active.value = true
    }
    const close = () => {
      active.value = false
    }
    const toggleDialog = () => {
      if (active.value) {
        close()
      } else {
        open()
      }
    }
    const dataList = ref([])
    const tempList = ref([])
    async function loadCityList() {
      const res = await axios({
        method: 'get',
        url: 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
      })
      dataList.value = res.data
      tempList.value = res.data
    }
    loadCityList()

    const changeResult = reactive({
      provinceCode: '', // 省code
      provinceName: '', // 省名称
      cityCode: '', // 城市code
      cityName: '', // 城市名称
      countyCode: '', // 地区code
      countyName: '', // 地区名
      resultAddress: '' // 最终结果地址
    })

    const optionClick = (item) => {
      if (item.level === 0) {
        changeResult.provinceCode = item.code
        changeResult.provinceName = item.name
      }
      if (item.level === 1) {
        changeResult.cityCode = item.code
        changeResult.cityName = item.name
      }
      // 当前点击的区
      if (item.level === 2) {
        changeResult.countyCode = item.code
        changeResult.countyName = item.name
        changeResult.resultAddress =
          changeResult.provinceName +
          '-' +
          changeResult.cityName +
          '-' +
          changeResult.countyName
        close()
        context.emit('change', changeResult)
      }
      dataList.value = item.areaList
    }

    watch(
      () => active.value,
      (newValue) => {
        if (newValue) {
          dataList.value = tempList.value

          changeResult.provinceCode = ''
          changeResult.provinceName = ''
          changeResult.cityCode = ''
          changeResult.cityName = ''
          changeResult.countyCode = ''
          changeResult.countyName = ''
          changeResult.resultAddress = ''
        }
      }
    )
    return {
      active,
      toggleDialog,
      dataList,
      optionClick,
      changeResult
    }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 10px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
